package ar.com.instMessenger.bean.persona;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;

import org.primefaces.event.RowEditEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;

import ar.com.instMessenger.bean.Bean;
import ar.com.instMessenger.entity.Persona;
import ar.com.instMessenger.servicios.dao.personas.IPersonaDao;

@Named
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public class PersonaBean extends Bean {
	private static final long serialVersionUID = -8725702349262618823L;
	
	@Autowired
	IPersonaDao personaDAO;
	
	private List<Persona> personas;

	
	@PostConstruct
	public void postConstructor(){
		personas = personaDAO.getPersonas();
		
	}

	public String agregarPersona() {
		personas.add(new Persona());
		return null;
	}
	
	public void onEdit(RowEditEvent event) {

		Persona persona = (Persona) event.getObject();
		personaDAO.update(persona);
    	

        FacesMessage msg = new FacesMessage("Parametro Editado", persona.getApellido()+ ", " + persona.getNombre());  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
      
    public void onCancel(RowEditEvent event) {  
    	Persona persona = (Persona) event.getObject();
        FacesMessage msg = new FacesMessage("Parametro Cancelado", persona.getApellido()+ ", " + persona.getNombre());  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }

	public IPersonaDao getPersonaDAO() {
		return personaDAO;
	}

	public void setPersonaDAO(IPersonaDao personaDAO) {
		this.personaDAO = personaDAO;
	}

	public List<Persona> getPersonas() {
		return personas;
	}

	public void setPersonas(List<Persona> personas) {
		this.personas = personas;
	} 
	
	
}
